﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Globalization;
using Legazpi.Payroll.DataAccess.DataObjects.Enums;
using System.ComponentModel;
using System.Reflection;
using Legazpi.Payroll.BusinessLogic.Services;

namespace Web.Utils
{
    public static class DropDownHelper
    {
        
        public static void ConstructDateDropdowns(DropDownList month,DropDownList day ,DropDownList year, DateTime? selectedDate)
        {

            for (int i = 0; i < 12; i++)
            {
                var iDay = i + 1;
                month.Items.Add(new ListItem 
                {
                    Value = iDay.ToString("00"),
                    Text = CultureInfo.CurrentUICulture.DateTimeFormat.MonthNames[i].Substring(0,3),
                    Selected = selectedDate.HasValue && selectedDate.Value.Month == iDay
                });
                
            }

            for (int i = 1; i <= 31; i++)
            {
                day.Items.Add(new ListItem
                {
                    Value = i.ToString("00"),
                    Text = i.ToString("00"),
                    Selected = selectedDate.HasValue && selectedDate.Value.Day == i
                });
            }

            for (int i = DateTime.Now.Year - 65; i < DateTime.Now.Year + 1; i++)
            {
                year.Items.Add(new ListItem
                {
                    Value = i.ToString(),
                    Text = i.ToString(),
                    Selected = selectedDate.HasValue && selectedDate.Value.Year == i
                });
            }

        }

        #region ExtensionMethods
        public static void ToAdjAccountSignSelectList(this DropDownList dropdown, int selectedValue)
        {
            dropdown.Items.Clear();
            dropdown.Items.Add(new ListItem { Value = "0", Text = "minus(-)", Selected = selectedValue == 0 });
            dropdown.Items.Add(new ListItem { Value = "1", Text = "add(+)", Selected = selectedValue == 1 });
        }
        public static void ToAdjAccountTypeSelectList(this DropDownList dropdown, int selectedValue)
        {
            dropdown.Items.Clear();

            var source = Enum.GetValues(typeof(AccountType));
            var descriptionAttribute = typeof(DescriptionAttribute);

            foreach (var value in source)
            {
                FieldInfo field = value.GetType().GetField(value.ToString());
                var attrs = (DescriptionAttribute)field.GetCustomAttributes(descriptionAttribute, false).FirstOrDefault();
                dropdown.Items.Add(new ListItem
                {
                    Value = ((int)value).ToString(),
                    Text = attrs != null ? attrs.Description : value.ToString(),
                    Selected = (int)value == selectedValue
                });
            }
        }
        public static void ToPositionSelectList(this DropDownList dropdown, int selectedValue)
        {
            var _positionService = new PositionService();

            dropdown.Items.Clear();
            dropdown.Items.Add(new ListItem { Value = "0", Text = "--Select--" });
            dropdown.Items.AddRange(
                  _positionService.GetAll()
                  .Select(x =>
                          new ListItem
                          {
                              Value = x.Id.ToString(),
                              Text = x.Code,
                              Selected = x.Id == selectedValue
                          }).ToArray<ListItem>()
                 );
        }
        public static void ToSectorSelectList(this DropDownList dropdown, int selectedValue)
        {
            var _sectorService = new SectorService();

            dropdown.Items.Clear();
            dropdown.Items.Add(new ListItem { Value = "0", Text = "--Select--" });
            dropdown.Items.AddRange(
                  _sectorService.GetAll()
                  .Select(x =>
                          new ListItem
                          {
                              Value = x.Id.ToString(),
                              Text = x.Name + " [" + x.Client.Code + "]",
                              Selected = x.Id == selectedValue
                          }).ToArray<ListItem>()
                 );
        }
        public static void ToSectorSelectList(this DropDownList dropdown, int selectedValue,int clientId)
        {
            var _sectorService = new SectorService();

            dropdown.Items.Clear();
            dropdown.Items.Add(new ListItem { Value = "0", Text = "--Select--" });
            dropdown.Items.AddRange(
                  _sectorService.GetAll().Where(x => x.ClientId == clientId)
                  .Select(x =>
                          new ListItem
                          {
                              Value = x.Id.ToString(),
                              Text = x.Name,
                              Selected = x.Id == selectedValue
                          }).ToArray<ListItem>()
                 );
        }
        public static void ToEmployeeStatusSelectList(this DropDownList dropdown, int selectedValue)
        {
            dropdown.Items.Clear();

            var source = Enum.GetValues(typeof(EmployedStatus));
            var descriptionAttribute = typeof(DescriptionAttribute);

            foreach (var value in source)
            {
                FieldInfo field = value.GetType().GetField(value.ToString());
                var attrs = (DescriptionAttribute)field.GetCustomAttributes(descriptionAttribute, false).FirstOrDefault();
                dropdown.Items.Add(new ListItem
                {
                    Value = ((int)value).ToString(),
                    Text = attrs != null ? attrs.Description : value.ToString(),
                    Selected = (int)value == selectedValue
                });
            }
        }
        public static void ToPayrollPeriodSelectList(this DropDownList dropdown, int selectedValue)
        {
            var _payrollDateService = new PayrollDateService();

            dropdown.Items.Clear();
            dropdown.Items.Add(new ListItem { Value = "0", Text = "--Select--" });
            dropdown.Items.AddRange(
                  _payrollDateService.GetAll()
                  .Select(x =>
                          new ListItem
                          {
                              Value = x.Id.ToString(),
                              Text = string.Format("{0} - {1}", DateTimeHelper.FormatDateString(x.Startdate), DateTimeHelper.FormatDateString(x.Enddate)),
                              Selected = x.Id == selectedValue
                          }).ToArray<ListItem>()
                 );
        }
        public static void ToAdjustmentCodeSelectList(this DropDownList dropdown, int selectedValue)
        {
            var _adjustmentService = new AdjustmentService();

            dropdown.Items.Clear();
            dropdown.Items.Add(new ListItem { Value = "0", Text = "--Select--" });
            dropdown.Items.AddRange(
                  _adjustmentService.GetAll()
                  .Select(x =>
                          new ListItem
                          {
                              Value = x.Id.ToString(),
                              Text = string.Format("{0} {1}", x.Code, (x.IsAdditional ? "(+add)" : "(-minus)")),
                              Selected = x.Id == selectedValue
                          }).ToArray<ListItem>()
                 );
        }
        public static void ToAdvanceCodeSelectList(this DropDownList dropdown, int selectedValue)
        {
            var _adjustmentService = new AdjustmentService();

            dropdown.Items.Clear();
            dropdown.Items.Add(new ListItem { Value = "0", Text = "--Select--" });
            dropdown.Items.AddRange(
                  _adjustmentService.GetListCashAdvances()
                  .Select(x =>
                          new ListItem
                          {
                              Value = x.Id.ToString(),
                              Text = x.Code,
                              Selected = x.Id == selectedValue
                          }).ToArray<ListItem>()
                 );
        }
        #endregion

    }
}